{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "12aa35b0-8f98-45a2-8318-e51e956823b3",
   "metadata": {},
   "source": [
    "# 模型转换\n",
    "\n",
    "同济子豪兄 2023-6-25"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ad7a3db-1eef-4acf-9eb5-f7a696e203d4",
   "metadata": {},
   "source": [
    "## 方式一：在线模型转换平台\n",
    "\n",
    "http://aimo.aidlux.com\n",
    "\n",
    "账号：AIMOTC001\n",
    "\n",
    "密码：AIMOTC001"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9480ded4-326c-44d4-81d0-5ceb27e7d648",
   "metadata": {},
   "source": [
    "## 方式二：手动转换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c8271b2-5e80-4c7d-b923-da586045b88f",
   "metadata": {},
   "source": [
    "### 安装配置环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a74dfc7-ef68-4961-a39b-a9c16cc76e1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装 ONNX、ONNX Runtime、ONNXSIM\n",
    "!pip install onnx onnxruntime onnxsim -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "\n",
    "# 安装其它第三方工具包\n",
    "!pip install protobuf==3.20.1 -i https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8734dc1c-ae01-42be-a2d2-656eb32172f9",
   "metadata": {},
   "source": [
    "### 验证安装配置成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1c78865f-735b-4c35-a533-a7306221fa28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PyTorch 版本 1.10.0+cu113\n",
      "ONNX 版本 1.14.0\n",
      "ONNX Runtime 版本 1.14.1\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print('PyTorch 版本', torch.__version__)\n",
    "\n",
    "import onnx\n",
    "print('ONNX 版本', onnx.__version__)\n",
    "\n",
    "import onnxruntime as ort\n",
    "print('ONNX Runtime 版本', ort.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9309f99b-3ce9-486d-91c9-613285647faa",
   "metadata": {},
   "source": [
    "### 安装onnx2tflite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "faab8d32-2e04-40ff-b6d9-db2305597e00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除原有的文件夹（如有）\n",
    "!rm -rf onnx2tflite\n",
    "\n",
    "# 下载onnx2tflite工具包\n",
    "!git clone https://github.com/MPolaris/onnx2tflite.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a1edaaa9-1b6b-41db-92db-6f8d01d6290d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd onnx2tflite"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6f55579-4e2b-4537-9b24-c00be2cd153c",
   "metadata": {},
   "source": [
    "### 转换ImageNet1000图像分类模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e7516ecf-0ad1-4f26-b39b-93e10c99f9dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Checking 0/1...\n",
      "INFO:tensorflow:Assets written to: /tmp/tmpibm8orhm/assets\n",
      "INFO:tensorflow:Assets written to: /tmp/tmpibm8orhm/assets\n",
      "Estimated count of arithmetic ops: 4.745 G  ops, equivalently 2.373 G  MACs\n",
      "WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded\n",
      "INFO:converter running::tflite model elements' max error is 6.3181E-06, pass, tflite saved in ../ckpt/resnet18_imagenet.tflite\n"
     ]
    }
   ],
   "source": [
    "!python converter.py --weights '../ckpt/resnet18_imagenet.onnx' --outpath '../ckpt' --formats 'tflite'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab4abd27-60a3-4e64-a747-026e95b0b214",
   "metadata": {},
   "source": [
    "> 导出为`resnet18_imagenet.tflite`文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7383ccec-9796-45a4-90a3-049031ed4b77",
   "metadata": {},
   "source": [
    "### 转换30类水果图像分类模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5c767709-e8ca-493d-ba77-c25a0f1b54b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Checking 0/1...\n",
      "INFO:tensorflow:Assets written to: /tmp/tmp2vf2scrf/assets\n",
      "INFO:tensorflow:Assets written to: /tmp/tmp2vf2scrf/assets\n",
      "Estimated count of arithmetic ops: 4.744 G  ops, equivalently 2.372 G  MACs\n",
      "WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded\n",
      "INFO:converter running::tflite model elements' max error is 4.8876E-06, pass, tflite saved in ../ckpt/resnet18_fruit30.tflite\n"
     ]
    }
   ],
   "source": [
    "!python converter.py --weights '../ckpt/resnet18_fruit30.onnx' --outpath '../ckpt' --formats 'tflite'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16aa12dc-8f91-497d-adfe-0aea3863a033",
   "metadata": {},
   "source": [
    "> 导出为`resnet18_fruit30.tflite`文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e5bd633-8bc4-4c4f-8f54-8c4f55bdeb83",
   "metadata": {},
   "source": [
    "## 方式三：直接下载子豪兄转换好的模型文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cd34d8b5-48b3-4a58-81be-fc6bc6c08be7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TFLite\n",
    "!wget https://zihao-tutorial.obs.cn-east-3.myhuaweicloud.com/aidlux/ckpt/resnet18_imagenet.tflite -P ckpt\n",
    "!wget https://zihao-tutorial.obs.cn-east-3.myhuaweicloud.com/aidlux/ckpt/resnet18_fruit30.tflite -P ckpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c34be81",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
